package api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jukg
 * @date 2024/09/24 15:50
 * @description
 */
public class FayanApi {


    private static String ACCESS_TOKEN_URL = "https://api.cjbdi.com:8443/auth/api/token/getAccess?appKey=%s&appSecret=%s";
    private static String ACCESS_TOKEN_KEY = "FAYAN:TOKEN:";

    private static String appId ="b65a87716dab43f88c74739df38c4949";
    private static String appKey ="a717b33649811f444b3a52dfc0be5164";
    private static String apiSecret ="ad5f1ee99fa92016eb9d25f16e3706e3";

    private static String Token ="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NjYwMDdhMWYzZDdlYTg2Nzc2NjhlZmUi" +
            "LCJ0ZWFtSWQiOiI2NjYwMDdhMWYzZDdlYTg2Nzc2NjhmMDEiLCJ0bWJJZCI6IjY2NjAwN2ExZjNkN2VhODY3NzY2OGYwMyIsImV4cCI6OD" +
            "gxMjIyMTY4MDUsImlhdCI6MTcyMjMwMzIwNX0.VZiGmQS4iyxs20gljgrrFeBPWK9z3eFdOstcTIw3U5U";

    public static String getToken(){
        String url = String.format(ACCESS_TOKEN_URL, appKey, apiSecret);

        HttpRequest request = HttpUtil.createPost(url);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appKey", appKey);
        jsonObject.put("apiSecret", apiSecret);
        request.body(JSON.toJSONString(jsonObject));
        HttpResponse httpResponse = request.execute();
        System.out.println(JSON.toJSONString(httpResponse.body()));
        return httpResponse.body();
    }

/*    public static void main(String[] args) {
        getToken();
    }*/

    public static void main2(String[] args) {

        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("http://10.1.234.216:7000/api/v1/chat/completions");
        try {
            post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
            post.addHeader("Token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NjYwMDdhMWYzZDdlYTg2Nzc2NjhlZmUiLCJ0ZWFtSWQiOiI2NjYwMDdhMWYzZDdlYTg2Nzc2NjhmMDEiLCJ0bWJJZCI6IjY2NjAwN2ExZjNkN2VhODY3NzY2OGYwMyIsImV4cCI6ODgxMjIyMTY4MDUsImlhdCI6MTcyMjMwMzIwNX0.VZiGmQS4iyxs20gljgrrFeBPWK9z3eFdOstcTIw3U5U");
            List<NameValuePair> params = new ArrayList<>();


            JSONObject param = new JSONObject();

            JSONObject j1 = new JSONObject();
            j1.put("dataId","I1IBE89MK37GDMH0CEE5LJA4");
            j1.put("role","user");
            j1.put("content","");
            JSONObject j2= new JSONObject();
            j2.put("dataId","9ME90A04N8I3BIJNCEDDBNHH");
            j2.put("role","assistant");
            j2.put("content","在吗");
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(j1);
            jsonArray.add(j2);
            param.put("messages", jsonArray);
            param.put("appId", "6683e2bcbef4d6ffc470f450");
            param.put("chatId", "7FE56EA6F7A4");

            JSONObject variables = new JSONObject();
            variables.put("cTime", DateUtil.date().toString());
            param.put("variables", variables);
            param.put("detail", true);
            param.put("stream", true);
            System.out.println(param.toJSONString());
            StringEntity s = new StringEntity(param.toJSONString(), ContentType.APPLICATION_JSON);
            s.setContentEncoding("UTF-8");
            post.setEntity(s);
            CloseableHttpResponse response = client.execute(post);
            String res = EntityUtils.toString(response.getEntity());
            System.out.println("response: " + res);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void main1(String[] args) {

        HttpRequest httpRequest = HttpUtil.createPost("http://10.1.234.216:7000/api/v1/chat/completions");
        httpRequest.header("Token", Token);
        httpRequest.contentType(cn.hutool.http.ContentType.JSON.getValue());
        JSONObject param = new JSONObject();

        JSONObject j1 = new JSONObject();
        j1.put("dataId","I1IBE89MK37GDMH0CEE5LJA4");
        j1.put("role","user");
        j1.put("content","在吗");
        JSONObject j2= new JSONObject();
        j2.put("dataId","9ME90A04N8I3BIJNCEDDBNHH");
        j2.put("role","assistant");
        j2.put("content","");
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(j1);
        jsonArray.add(j2);
        param.put("messages", jsonArray);
        param.put("appId", "6683e2bcbef4d6ffc470f450");
        param.put("chatId", "7FE56EA6F7A4");

        JSONObject variables = new JSONObject();
        variables.put("cTime", DateUtil.date().toString());
        param.put("variables", variables);
        param.put("detail", true);
        param.put("stream", true);

        httpRequest.body(param.toJSONString());
        HttpResponse response = httpRequest.execute();
        System.out.println("response:" + response.body());

    }

    public static void main(String[] args) {
        double x = -10;
        assert x > 0 : "error";
        System.out.println(x);
    }
}
